package com.boxin.ims.modules.wechat.busi.game;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.busi.BusinessProcessor;
import com.boxin.ims.modules.wechat.busi.ProcessMessage;
import com.boxin.ims.modules.wechat.entity.Lottery;
import com.boxin.ims.modules.wechat.entity.LotteryLog;
import com.boxin.ims.modules.wechat.entity.LotteryRecord;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.entity.game.NumberGame;
import com.boxin.ims.modules.wechat.entity.game.NumberGameConfig;
import com.boxin.ims.modules.wechat.service.LotteryLogService;
import com.boxin.ims.modules.wechat.service.LotteryRecordService;
import com.boxin.ims.modules.wechat.service.game.NumberGameConfigService;
import com.boxin.ims.modules.wechat.service.game.NumberGameService;

/**
 * 
 * @author Administrator
 * @date 2014-8-22
 * @function <p> 现场数字互动游戏 </p>
 */
@Service
public class NumberProcessor extends BusinessProcessor{

	@Autowired
	NumberGameService numberGameService;
	
	@Autowired
	NumberGameConfigService numberGameConfigService;
	
	@Autowired
	LotteryRecordService lotteryRecordService;
	
	
	@Autowired
	LotteryLogService lotteryLogService;
	
	
	@Override
	public ProcessMessage process() {
		
		WechatMember member = memberCheckAndReply();
		if(member != null){
			NumberGameConfig config =  numberGameConfigService.getByUserId(user.getId());
			
			
			
			NumberGame 	numberGame = new NumberGame();
			numberGame.setUser(user);
			numberGame.setStatus(NumberGame.STATUS_NORMAL);
			
			List<NumberGame> gameList	= numberGameService.find( numberGame);
			
			if(gameList != null && !gameList.isEmpty()){			//有激活的游戏数字项
				numberGame = gameList.get(0);
				String number = numberGame.getNumber();
				
				LotteryLog query = new LotteryLog();
				query.setMember(member);
				query.setSourceId(numberGame.getId());
				
				List<LotteryLog> logList =	lotteryLogService.find(query);
				if(logList!=null && !logList.isEmpty()){	//已参与过
					
					writeImageToUser("温馨提示", "  您已经参与过一次这轮的游戏了，请关注下一轮比赛~~", "", "");
					return null;
				}
				
				
				
				if(number.equals(textMessage.getContent())){	//中奖了
					
					LotteryRecord rQuery = new LotteryRecord();
					rQuery.setSourceId(numberGame.getId());
					
					List<LotteryRecord> recordList =	lotteryRecordService.find(rQuery);
					
					if(recordList!=null && recordList.size() > config.getLotteryCount()){		//超出中奖者范围
						writeImageToUser("温馨提示", "  亲，奖品已发送完毕,请参与下一轮游戏...", "", "");
						
						generateLotteryLog(member, null, numberGame.getId());
						return null;
					}
					
					LotteryRecord record = new LotteryRecord();
					record.setEndTime(DateUtils.getDelayDate(config.getValidDays()));
					record.setLottery(null);
					record.setMember(member);
					record.setName(numberGame.getName());
					record.setSourceId(numberGame.getId());
					record.setSourceType( LotteryRecord.SOURCE_TYPE_ME);
					record.setStatus(LotteryRecord.STATUS_NORMAL);
					record.setUser(user);
					
					//中奖记录
					lotteryRecordService.save(record);	
					
					writeImageToUser("恭喜您中奖啦", "  亲，恭喜您赢得了本轮魔幻数字互动游戏的【"+numberGame.getName()+"】奖品,请保存本消息的二维码并于有效期("+config.getValidDays()+"日)内使用,谢谢！", record.getQrCode().getId());
					//saveLotteryLog(member, lottery);	//中奖日志
					
					generateLotteryLog(member, record, numberGame.getId());
					
					return null;
					
				}else{	
					//未中奖
					writeImageToUser("很遗憾未中奖", "亲，您这次未能中奖，继续加油!!", "", "");
					
					generateLotteryLog(member, null, numberGame.getId());
				}
				
			}else{
				writeImageToUser("活动未开始,请稍等...", "", "", "");
			}
			
			
			
			
			
		}
		
		return null;
	}
	


	/**
	 * @author Jakemanse
	 * @time 2014-8-22  下午5:57:59
	 * @function <p> 抽奖日志 </p>
	 * @param member
	 * @param mylottery
	 * @param sourceId
	 */
	public void generateLotteryLog(WechatMember member,	LotteryRecord mylottery,Long sourceId){
			LotteryLog lotteryLog = new LotteryLog();
			lotteryLog.setMember(member);
			lotteryLog.setSourceId(sourceId);
			lotteryLog.setName((mylottery==null)?null:mylottery.getName());
			lotteryLog.setUser(user);
			lotteryLog.setStatus(LotteryLog.STATUS_NORMAL);
			lotteryLog.setRemarks((mylottery==null)?"未中奖":mylottery.getName());
			lotteryLogService.save(lotteryLog);
	}

	@Override
	public boolean isNeedBeMember() {
		
		return false;
	}

}
